//
//  SSMineHeadView.m
//  textDemo
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSMineHeadView.h"

@interface SSMineHeadView()
@property(nonatomic, strong) UIImageView *headBackImage;
@property(nonatomic, strong) UIImageView *iconImage;        // 用户头像
@property(nonatomic, strong) UILabel *userNameLabel;        // 用户昵称
@property(nonatomic, strong) UILabel *lvLabel;              // 用户等级


@end

@implementation SSMineHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    [self addSubview:self.headBackImage];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    [self addSubview:self.iconImage];
    [self addSubview:self.userNameLabel];
    [self addSubview:self.lvLabel];
    
    #pragma mark - 约束
    __weak typeof (self) weakSelf = self;
    [_headBackImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(60);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(-60);
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
        
    }];
    
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(75, 75));
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.left.equalTo(weakSelf.mas_left).offset(60);
    }];
    
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImage.mas_right).offset(39);
        make.height.mas_equalTo(16);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.iconImage.mas_top).offset(12);
    }];
    
    [_lvLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImage.mas_right).offset(39);
        make.right.equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(16);
        make.bottom.equalTo(weakSelf.iconImage.mas_bottom).offset(-12);
    }];
    
}

#pragma mark -
#pragma mark - 登录&注册
- (void)clickRegisterButton{
    if (_registerBlock) {
        _registerBlock();
    }
    
}

- (void)clickLoginButton{
    if (_loginBlock) {
        _loginBlock();
    }
}

#pragma mark -
#pragma mark - 刷新headView
- (void)reloadHeadView{
    NSDictionary *loginDict = [[NSUserDefaults standardUserDefaults] valueForKey:@"ISLOGIN"];
    if (!loginDict.count) {
        _loginButton.hidden = YES;
        _registerButton.hidden = YES;
        
        //登录后
        _iconImage.hidden = NO;
        _userNameLabel.hidden = NO;
        _lvLabel.hidden = NO;
        
    }else{
        
        _loginButton.hidden = NO;
        _registerButton.hidden = NO;
        
        //登录后
        _iconImage.hidden = YES;
        _userNameLabel.hidden = YES;
        _lvLabel.hidden = YES;
        
    }
}

#pragma mark -
#pragma mark - 懒加载

- (UIImageView *)headBackImage{
    if (!_headBackImage) {
        _headBackImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"我的背景"]];
        
    }
    return _headBackImage;
    
    
}

- (UIButton *)registerButton{
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        _registerButton.titleLabel.textColor = [UIColor whiteColor];
         [_registerButton addTarget:self action:@selector(clickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return _registerButton;
}

- (UIButton *)loginButton{
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        _loginButton.titleLabel.textColor = [UIColor whiteColor];
        [_loginButton addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return  _loginButton;
}


- (UIImageView *)iconImage{
    if (!_iconImage) {
        _iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icomImage"]];
        
    }
    return _iconImage;
}

- (UILabel *)userNameLabel{
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] init];
        _userNameLabel.text = @"用户昵称";
    }
    return _userNameLabel;
}

- (UILabel *)lvLabel{
    if (!_lvLabel) {
        _lvLabel = [[UILabel alloc]init];
        _lvLabel.text = @"普通用户";
        
    }
    return _lvLabel;
}
@end
